package com.cg;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.baomidou.mybatisplus.generator.config.rules.NamingStrategy.capitalFirst;

public class MpGenerator {

    public static DataSourceConfig.Builder datasource() {
        return new DataSourceConfig.Builder("jdbc:postgresql://192.168.10.233:5432/production", "postgres", "2021.Hj")
                .schema("public")
                .typeConvert(new MyPostgreSqlTypeConvert());
    }

    public static TemplateConfig.Builder templateConfig() {
        return new TemplateConfig.Builder()
                .controller("")
                .service("");
    }

    public static Map<OutputFile, String> packageMap(String packagePath) {
        Map<OutputFile, String> map = new HashMap<>();
        map.put(OutputFile.mapperXml, "src\\main\\resources\\mapper");
        map.put(OutputFile.entity, "src\\main\\java\\" + packagePath + "\\entity");
        map.put(OutputFile.mapper, "src\\main\\java\\" + packagePath + "\\mapper");
        return map;
    }

    public static String underlineToCamel(String name) {
        // 快速检查
        if (StringUtils.isBlank(name)) {
            // 没必要转换
            return StringPool.EMPTY;
        }
        String tempName = name;
        // 大写数字下划线组成转为小写 , 允许混合模式转为小写
        if (StringUtils.isCapitalMode(name) || StringUtils.isMixedMode(name)) {
            tempName = name.toLowerCase();
        }
        StringBuilder result = new StringBuilder();
        // 用下划线将原始字符串分割
        String[] camels = tempName.split(ConstVal.UNDERLINE);
        // 跳过原始字符串中开头、结尾的下换线或双重下划线
        // 处理真正的驼峰片段
        Arrays.stream(camels).filter(camel -> !StringUtils.isBlank(camel)).forEach(camel -> {
            if (result.length() == 0) {
                // 第一个驼峰片段，首字母都小写
                result.append(StringUtils.firstToLowerCase(camel));
            } else {
                // 其他的驼峰片段，首字母大写
                result.append(capitalFirst(camel));
            }
        });
        return result.toString();
    }

    public static void main(String[] args) {
        FastAutoGenerator.create(datasource())
                // 全局配置
                .globalConfig((scanner, builder) -> {
//                    builder.author(scanner.apply("请输入创建人姓名"))
//                            .fileOverride()
//                            .outputDir(System.getProperty("user.dir"))
//                            .disableOpenDir();
                    builder.author("cheyinbo")
                            .fileOverride()
                            .outputDir(System.getProperty("user.dir"))
                            .disableOpenDir();
                })
                // 包配置
                .packageConfig((scanner, path) -> {
//                    String packagePath = scanner.apply("请输入包路径(com.xxx.xxx)");
                    String packagePath = "com.cg";
                    path.pathInfo(packageMap(packagePath.replace(".", "/")))
                            .parent(packagePath);
                })
                // 策略配置
                .strategyConfig((scanner, builder) -> {
//                    builder.addInclude(scanner.apply("请输入表名")).addTablePrefix("t_")
//                            .enableSchema()
//                            .mapperBuilder()
//                            .formatXmlFileName("%sMapper")
//                            .formatMapperFileName("%sMapper")
//                            .enableBaseResultMap()
//                            .enableBaseColumnList()
//                            .entityBuilder()
//                            .formatFileName("%sPO")
//                            .enableLombok()
//                            .enableTableFieldAnnotation()
//                            .idType(IdType.AUTO)
//                            .disableSerialVersionUID();
                    builder.addInclude("production_in_order_item").addTablePrefix("t_")
                            .enableSchema()
                            .mapperBuilder()
                            .formatXmlFileName("%sMapper")
                            .formatMapperFileName("%sMapper")
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .entityBuilder()
                            .formatFileName("%sPO")
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            .disableSerialVersionUID()
                            .enableUseJdbcType()
                            .enableUseArrayHandler();
                })
                //生成模板
                .templateConfig(template -> {
                    template.controller("")
                            .service("")
                            .serviceImpl("");
                })
                .execute();
    }

}